from DrissionPage import ChromiumPage, ChromiumOptions
import time

class kuaiMaiErp:
    def __init__(self):
        self.page = ChromiumPage()
        self.iframe = None
        # 配置
        self.baseLoadTime = 2
        self.douyinLoadTime = 15
        self.taobaoLoadTime = 15

        self.pinduoduoLoadTime = 5
        self.xianyuLoadTime = 3
        self.saveTime = 5   # 保存时间
        self.refreshTime = 10   # 刷新后到加载时间

    def login(self):
        self.page.get("https://erpb.superboss.cc/index.html#/prod/parallel/")

    def edit(self):
        while 1:
            self.iframe = self.page.get_frame('xpath://iframe[@title="商品中心"]')
            # 记录当前页面
            currentPageNumber = self.iframe.ele('xpath://li[@class="number active"]').texts()[0]
            print("*"*10 + '第'+str(currentPageNumber)+'页' + '*'*10)
            # 启动
            for idx, item in enumerate(self.iframe.eles('xpath://button[@class="el-button el-button--link"]/span[text()="编辑"]')):
                print("-"*10 + str(idx+1) + '-'*10)
                item.click()
                self.editBase()
                # self.editDouyin()
                # self.editTaobao()
                self.editPindduoduo()
                self.editXianyu()
                # 点击关闭按钮并等待保存时间
                self.iframe.ele('xpath://button[@aria-label="close 商品编辑"]').click()
                time.sleep(2)
                try:
                    if self.iframe.ele('xpath://button[@aria-label="close 商品编辑"]'):
                        self.iframe.ele('xpath://button[@aria-label="close 商品编辑"]').click()
                except:
                    print('已经关闭编辑页，无需再次关闭')
                time.sleep(self.saveTime)
            
            # 网页刷新
            self.page.refresh()
            time.sleep(self.refreshTime)
            # 跳转到当前页面的下一个页面
            self.iframe = self.page.get_frame('xpath://iframe[@title="商品中心"]')
            self.iframe.ele('xpath://span[@class="el-pagination__jump"]//input').clear()
            time.sleep(1)
            self.iframe.ele('xpath://span[@class="el-pagination__jump"]//input').clear()
            self.iframe.ele('xpath://span[@class="el-pagination__jump"]//input').input(str(int(currentPageNumber)+1))
            self.page.ele('xpath://li[@data-name="商品铺货"]').click()
            time.sleep(self.refreshTime)
    
    def editBase(self):
        # 点击基础资料
        print(f"基础资料加载时间: {self.baseLoadTime}s")
        time.sleep(self.baseLoadTime)

        # 点击保存并等待保存时间
        self.iframe.ele('xpath://div[@class="left"]//button[@class="el-button el-button--primary"]').click()
        time.sleep(self.saveTime)

    def editDouyin(self):
        # 点击抖音资料
        self.iframe.ele('xpath://div[text()="抖音资料"]').click()
        print(f"抖音资料加载时间: {self.douyinLoadTime}s")
        time.sleep(self.douyinLoadTime)

        # 帮帮家甄选
        print("判断是否帮帮家甄选选项被填充")
        self.iframe.ele('xpath://div[@class="el-form-item__content"]//input[@placeholder="请选择"]').click()
        self.iframe.ele('xpath://span[text()="无品牌"]').click(by_js=True)

        # 厚度
        print("判断是否厚度选项被填充")
        if not self.iframe.ele('xpath://div[@class="attr-item"][2]//ul/li[@class="el-select-dropdown__item selected hover"]', timeout=3):
            self.iframe.ele('xpath://div[@class="attr-item"][2]//input[@placeholder="请选择"]').click()
            self.iframe.ele('xpath://span[text()="薄款"]').click(by_js=True)
        # 服装版型
        print("判断是否服装版型选项被填充")
        if not self.iframe.ele('xpath://div[@class="attr-item"][6]//ul/li[@class="el-select-dropdown__item selected hover"]', timeout=6):
            self.iframe.ele('xpath://div[@class="attr-item"][6]//input[@placeholder="请选择"]').click()
            self.iframe.ele('xpath://span[text()="宽松型"]').click(by_js=True)

        # 面料材质
        ## 材质
        self.iframe.ele('xpath://div[@class="attr-item multi-measure-item"]//div[@class="el-form-item"][1]//input[@placeholder="请选择"]').click()
        """ 第0个指的是里料材质; 第1是辅料材质; 第2才是面料材质。【没有位置和大小：采用by_js进行点击】 """
        if len(self.iframe.eles('xpath://span[text()="聚酯纤维"]')) > 1:
            self.iframe.eles('xpath://span[text()="聚酯纤维"]')[2].click(by_js=True)
        else:
            self.iframe.ele('xpath://span[text()="聚酯纤维"]').click(by_js=True)

        ## 含量
        self.iframe.ele('xpath://div[@class="attr-item multi-measure-item"]//div[@class="el-form-item"][2]//input[1]').input('100', clear=True)

        # 尺码图
        self.iframe.ele('xpath://span[@class="el-radio__label" and text()="尺码图"]').click()
        print("判断是否有提示没上传尺码图")
        if self.iframe.ele('xpath://div[@class="el-form-item__error" and contains(text(), "请上传尺码图")]', timeout=3):
            self.iframe.ele('xpath://div[@class="material-space-upload" and text()="素材空间上传"]').click(by_js=True)
            print("等待3s商品素材空间刷新")
            time.sleep(3)
            materials_iframe = self.page.get_frame('xpath://iframe[@title="商品素材空间"]')
            materials_iframe.ele('xpath://div[text()="尺码表"]').click.multi(2)
            materials_iframe.ele('xpath://div[text()="11.jpeg"]').click(by_js=True)
            self.page.eles('xpath://div[@class="el-dialog__footer"]/div/button[@class="el-button el-button--primary"]/span[text()="确定"]')[2].click()

        # 点击保存并等待保存时间
        self.iframe.ele('xpath://div[@class="left"]/button[@class="el-button el-button--primary"]').click()
        time.sleep(self.saveTime)
    
    
    def editTaobao(self):
        # 点击淘宝资料
        self.iframe.ele('xpath://div[text()="淘宝资料"]').click()
        print(f"淘宝资料加载时间: {self.taobaoLoadTime}s")
        time.sleep(self.taobaoLoadTime)

        # 面料成分
        ## 材质
        self.iframe.ele('xpath://span[text()="新型聚酯纤维"]').click(by_js=True)
        ## 含量
        self.iframe.ele('xpath://div[@class="multi-complex-items_item"]/div[contains(@class, "el-form-item")][2]//input[@name="material_prop_content"]').input('100', clear=True)

        # 参数: 身高、体重
        if "is-checked" in self.iframe.ele('xpath://input[@value="sizeMapping_shengao"]').parent().attr('class'):
            self.iframe.ele('xpath://input[@value="sizeMapping_shengao"]').click()
        if "is-checked" in self.iframe.ele('xpath://input[@value="sizeMapping_tizhong"]').parent().attr('class'):
            self.iframe.ele('xpath://input[@value="sizeMapping_tizhong"]').click()
        print("勾选身高体重")
        time.sleep(2)
        self.iframe.ele('xpath://input[@value="sizeMapping_shengao"]').click()
        self.iframe.ele('xpath://input[@value="sizeMapping_tizhong"]').click()
        ## 切换单位->值【使用遍历会有问题】
        self.iframe.eles('xpath://thead[@class="has-gutter"]//button/span')[0].click()
        self.iframe.eles('xpath://thead[@class="has-gutter"]//button/span')[1].click()
        ## 填入最小值最大值【目前是两组，如果是三组，就把4改为6。。待完善】
        for i in range(0, 4):
            if i % 2 == 0:
                self.iframe.eles('xpath://tr[contains(@class, "el-table__row")]//input[@placeholder="最小值"]')[i].input("140")
                self.iframe.eles('xpath://tr[contains(@class, "el-table__row")]//input[@placeholder="最大值"]')[i].input("170")
            else:
                self.iframe.eles('xpath://tr[contains(@class, "el-table__row")]//input[@placeholder="最小值"]')[i].input("40")
                self.iframe.eles('xpath://tr[contains(@class, "el-table__row")]//input[@placeholder="最大值"]')[i].input("60")


        # 点击保存并等待保存时间
        self.iframe.ele('xpath://div[@class="left"]/button[@class="el-button el-button--primary"]').click()
        time.sleep(self.saveTime)

    def editPindduoduo(self):
        # 点击拼多多资料
        self.iframe.ele('xpath://div[text()="拼多多资料"]').click()
        time.sleep(2)
        self.iframe.ele('xpath://div[text()="拼多多资料"]').click()
        print(f"拼多多资料加载时间: {self.pinduoduoLoadTime}s")
        time.sleep(self.pinduoduoLoadTime)

        # 单价
        print('\t判断单价是否合格')
        if self.iframe.ele('xpath://div[@class="el-form-item__error" and contains(text(), "不允许小于单买价的最大值+1")]', timeout=2):
            self.iframe.ele('xpath://label[@for="marketPrice"]/../div[@class="el-form-item__content"]//input').input('9', clear=True, by_js=True)
            time.sleep(1)
            self.iframe.ele('xpath://label[@for="marketPrice"]/../div[@class="el-form-item__content"]//input').input('9')
            time.sleep(1)
            self.iframe.ele('xpath://label[@for="marketPrice"]/../div[@class="el-form-item__content"]//input/..').click(by_js=True)
        
        # 点击保存并等待保存时间
        self.iframe.ele('xpath://div[@class="left"]//button[@class="el-button el-button--primary"]').click()
        time.sleep(self.saveTime)

    def editXianyu(self):
        # 点击闲鱼资料
        self.iframe.ele('xpath://div[text()="闲鱼资料"]').click()
        time.sleep(2)
        self.iframe.ele('xpath://div[text()="闲鱼资料"]').click()
        print(f"闲鱼资料加载时间: {self.xianyuLoadTime}s")
        time.sleep(self.xianyuLoadTime)

        # 修改类目
        print("\t判断类目是否合格")
        if not self.iframe.ele('xpath://div[@class="platform-category-input" and text()=" 潮品 > 女装 > 其他女装 > 其他女装 "]', timeout=2):
            self.iframe.ele('xpath://button/span[text()="修改类目"]').click()
            time.sleep(1)
            self.iframe.ele('xpath://span[text()=" 潮品"]').click()
            time.sleep(0.8)
            self.iframe.ele('xpath://span[text()=" 女装"]').click()
            time.sleep(0.8)
            self.iframe.ele('xpath://span[text()=" 其他女装"]').click()
            time.sleep(0.8)
            self.iframe.eles('xpath://span[text()=" 其他女装"]')[1].click()
            time.sleep(0.8)
            self.iframe.ele('xpath://div[@aria-label="修改类目"]//button[@class="el-button el-button--primary"]/span[text()="确 定"]').click()
            time.sleep(self.saveTime)

        # 修改成色
        self.iframe.ele('xpath://span[text()="有成色"]').click(by_js=True)
        time.sleep(0.8)
        self.iframe.ele('xpath://span[@class="el-radio__label" and text()="全新"]').click()

        # 库存数
        print("\t判断库存数是否合格")
        kucun = self.iframe.ele('xpath://div[@class="el-form-item__error" and contains(text(), "库存数需要大于0")]', timeout=2)
        if kucun:
            kucun.prev().ele('xpath://input').input('9', clear=True, by_js=True)
            time.sleep(1)
            kucun.prev().ele('xpath://input').input('99')
        
        # 点击保存并等待保存时间
        self.iframe.ele('xpath://div[@class="left"]//button[@class="el-button el-button--primary"]').click()
        time.sleep(self.saveTime)

if __name__ == '__main__':
    try:
        kuaimai = kuaiMaiErp()
        kuaimai.login()
        input("请登录完后进入铺货页面回车继续")

        kuaimai.edit()
    except:
        input("已中断")


